package com.zjy.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zjy.basic.domain.dto.OrderAdoptDto;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.CodeGenerateUtils;
import com.zjy.basic.util.CurrentLoginInfoUtil;
import com.zjy.order.domain.OrderAdopt;
import com.zjy.order.mapper.OrderAdoptMapper;
import com.zjy.order.service.OrderAdoptService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.pet.domain.Pet;
import com.zjy.pet.mapper.PetMapper;
import com.zjy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements OrderAdoptService {
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public AjaxResult submitOrder(Pet pet, HttpServletRequest request) throws Exception {
        //拿到登录人(下单用户)的信息
        Logininfo logininfo = CurrentLoginInfoUtil.currentLoginInfo(request);
        //生成领养订单
        OrderAdopt oa = new OrderAdopt();
        oa.setDigest("对"+pet.getName()+"的领养订单");
        oa.setPrice(pet.getSaleprice());
        //没有需求，附加内容暂时写死
        oa.setServiceMethod(0);
        oa.setPetId(pet.getId());
        oa.setUserId(43L);
//        oa.setUserId(logininfo.getId());
        oa.setShopId(pet.getShopId());
        //
        oa.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        oa.setPaySn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        orderAdoptMapper.add(oa);

        //成功，将宠物下架并添加宠物信息的userId
        Pet pet1 = petMapper.findById(pet.getId());
        //下架
        pet1.setState(0);
        pet1.setUserId(43L);
//        oa.setUserId(logininfo.getId());
        pet1.setOffsaletime(new Date());
        petMapper.update(pet1);
        return AjaxResult.me();
    }
}
